/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-02-17
*/
package com.rzico.basics.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Member;
import com.rzico.basics.entity.Product;
import com.rzico.basics.model.*;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.ProductService;
import com.rzico.core.entity.SysUser;
import com.rzico.core.service.SysUserService;
import com.rzico.entity.Filter;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import com.rzico.basics.entity.Shop;
import com.rzico.basics.service.ShopService;
import com.rzico.util.AreaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;
import com.rzico.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

/**
 * 店铺/仓库控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-02-17
 */
@Api(description = "店铺/仓库接口")
@RestController
@RequestMapping("/shop")
public class
ShopController extends BaseController {

    @Autowired
    private ShopService shopService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ProductService productService;

    /**
     * 获取商户的店铺
     *
     * @return
    */
    @ApiOperation("获取商户的店铺")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mchId", value = "商户id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "lat", value = "lat", dataType = "Double", paramType = "query"),
            @ApiImplicitParam(name = "lng", value = "lng", dataType = "Double", paramType = "query"),
            @ApiImplicitParam(name = "productId", value = "商品Id", dataType = "Long", paramType = "query")
    })
    public CommResult<Shop> list(Double lat,Double lng,String mchId,Integer type, Pageable pageable, Long productId) {
        Map<String, Object> params = new HashMap<String, Object>();

        Enterprise enterprise = enterpriseService.findByMch(mchId);
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }

        params.put("type",0);

        if (productId!=null) {
            params.put("supplierId",productService.selectByPrimaryKey(productId).getSupplierId());
        }

        params.put("deleted",false);

        List<ShopVo> list = shopService.selectLbsList(params,lat,lng);
        return CommResult.success(list);
    }

    /**
     * 获取服务商下的店铺
     *
     * @return
     */
    @ApiOperation("获取服务商下的店铺")
    @GetMapping("/isvList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "isvId", value = "ISVid", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "lat", value = "lat", dataType = "Double", paramType = "query"),
            @ApiImplicitParam(name = "lng", value = "lng", dataType = "Double", paramType = "query"),
    })
    public CommResult<Shop> isvList(Double lat,Double lng,String isvId, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();

        params.put("type",0);
        params.put("isvId",isvId);

        params.put("deleted",false);

        List<ShopVo> list = shopService.selectLbsList(params,lat,lng);
        return CommResult.success(list);
    }



    /**
     * 获取附近店铺列表
     *
     * @return
     */
    @ApiOperation("获取附近店铺列表")
    @GetMapping("/nlist")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaId", value = "所在区域", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "scopeType", value = "商户类型", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "shopCategoryId", value = "分类ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "lat", value = "lat", dataType = "Double", paramType = "query"),
            @ApiImplicitParam(name = "lng", value = "lng", dataType = "Double", paramType = "query"),
            @ApiImplicitParam(name = "tagIds", value = "标签过滤", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "关键字", dataType = "String", paramType = "query")
    })
    public CommResult<Shop> nList(Double lat,Double lng,Long areaId,Long shopCategoryId,Integer scopeType,Long[] tagIds,String keyword, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();

        params.put("type",0);

        if (areaId!=null){
            AreaUtils.encodeMap(areaId,params);
        }

        if (scopeType!=null) {
            params.put("scopeType", scopeType);
        }

        if (tagIds!=null && tagIds.length>0) {
            params.put("tagIds",tagIds);
        }

        if (StringUtils.isNotEmpty(keyword)) {
            params.put("keyword",keyword);
        }

        if (shopCategoryId!=null) {
            params.put("treePath",shopCategoryId);

        }

        params.put("deleted",false);
        params.put("status",1);

        List<ShopVo> list = shopService.selectLbsList(params,lat,lng);

        Integer startRow = (pageable.getPageNum()-1)*pageable.getPageSize();
        Integer endRow = startRow+pageable.getPageSize();
        if (endRow>list.size()) {
            endRow = list.size();
        }
        PageResult<ShopVo> pageResult = null;
        if (list.size()>0) {
            if (startRow<endRow) {
                pageResult = new PageResult<ShopVo>(list.subList(startRow, endRow), list.size(), pageable);
            } else {
                pageResult = new PageResult<ShopVo>(new ArrayList<ShopVo>(), list.size(), pageable);
            }
        } else {
            pageResult = new PageResult<ShopVo>(list, list.size(), pageable);
        }
        return CommResult.success(pageResult);
    }


    /**
     * 查询单条店铺详情
     *
     * @return
     */
    @ApiOperation("查询单条店铺详情")
    @GetMapping("/find/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lat", value = "lat", dataType = "Double", paramType = "query"),
            @ApiImplicitParam(name = "lng", value = "lng", dataType = "Double", paramType = "query"),
    })
    public CommResult<Product> find(@PathVariable String id,Double lat,Double lng) {

        if (lat==null) {
            lat = 0d;
        }
        if (lng==null) {
            lng = 0d;
        }

        Map<String, Object> params = new HashMap<String, Object>();

        params.put("id",id);

        List<ShopVo> list = shopService.selectLbsList(params,lat,lng);

        Map<String,Object> data = new HashMap<>();
        if (list.size()>0) {
            data.put("shop",list.get(0));
        } else {
            return CommResult.error("无效ID");
        }

        return CommResult.success(data);

    }

    /**
     * 查询单条店铺详情
     *
     * @return
     */
    @ApiOperation("查询推荐店铺")
    @GetMapping("/findAffiliated/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lat", value = "lat", dataType = "Double", paramType = "query"),
            @ApiImplicitParam(name = "lng", value = "lng", dataType = "Double", paramType = "query"),
    })
    public CommResult<Product> findAffiliated(@PathVariable String id,Double lat,Double lng) {

        if (lat==null) {
            lat = 0d;
        }
        if (lng==null) {
            lng = 0d;
        }

        Map<String, Object> params = new HashMap<String, Object>();

        params.put("id",id);

        List<ShopVo> list = shopService.selectLbsList(params,lat,lng);

        Map<String,Object> data = new HashMap<>();
        if (list.size()>0) {
            ShopVo shopVo = list.get(0);
            shopVo.setProducts(null);
            shopVo.setPromotions(null);
            data.put("shop",shopVo);
        } else {
            return CommResult.error("无效ID");
        }

        return CommResult.success(data);

    }



    /**
     * 保存新商户
     *
     * @param shop
     * @return
     */
    @ApiOperation("保存新商户")
    @PostMapping("/saveMchShop")
    public CommResult<ShopExtVo> saveMchShop(@RequestBody ShopExtVo shop, Long[] tagIds, String username, String password) {

        Enterprise enterprise = enterpriseService.getCurrent();
        shop.setEnterpriseId(enterprise.getId());
        shop.setDeleted(false);

        shopService.saveMchShop(shop,username,password,tagIds);
        return CommResult.success(shop);

    }

}
